#include "key.h"
#include "delay.h"
#include "exti.h"
#include "stm32f10x.h"
#include "smg.h"
//外部中断初始化函数
void EXTIX_Init(void)
{
 	EXTI_InitTypeDef EXTI_InitStructure;
 	NVIC_InitTypeDef NVIC_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);/*外部中断需要使能AFIO时钟*/
	Key_Init();//初始化按键对应IO模式
	//GPIOD.0	  中断线以及中断初始化配置
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource0);
	EXTI_InitStructure.EXTI_Line=EXTI_Line0;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;	
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);	 /*根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器*/
	 //GPIOD.1	  中断线以及中断初始化配置
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource1);
	EXTI_InitStructure.EXTI_Line=EXTI_Line1;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;	
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);	/*根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器*/
	//GPIOD.2	  中断线以及中断初始化配置
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource2);
	EXTI_InitStructure.EXTI_Line=EXTI_Line2;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;	
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);	/*根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器*/
	 //GPIOD.5	  中断线以及中断初始化配置
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource5);
	EXTI_InitStructure.EXTI_Line = EXTI_Line5;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;	
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);	/*根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器*/	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;/*使能按键所在的外部中断通道*/
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;//抢占优先级1 
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;	//响应优先级1
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure); //使能外部中断通道
	NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;/*使能按键所在的外部中断通道*/
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;//抢占优先级1 
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;//响应优先级1
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;	//使能外部中断通道
	NVIC_Init(&NVIC_InitStructure); 
	NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;/*使能按键所在的外部中断通道*/
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;//抢占优先级1
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;	//响应优先级1
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;	//使能外部中断通道
	NVIC_Init(&NVIC_InitStructure); 
}
void NVIC_Disable(void){ 
	__set_PRIMASK(1);
}
 void EXTI0_IRQHandler(void)
{			
	delay_ms(10);   //消抖			 
	if(KEY0_STA==0)	{
		SMG_display(1);
		NVIC_Disable();
	}
 	 EXTI_ClearITPendingBit(EXTI_Line0);    //清除LINE0上的中断标志位
}

void EXTI1_IRQHandler(void)
{
  delay_ms(10);    //消抖			 
  if(KEY1_STA==0)	{
		SMG_display(2);
	   NVIC_Disable();
	}
	 EXTI_ClearITPendingBit(EXTI_Line1);  //清除LINE1上的中断标志位
}
void EXTI2_IRQHandler(void)
{
  delay_ms(10);    //消抖			 
  if(KEY2_STA==0)	{
	  SMG_display(3);
	  NVIC_Disable();
	}
	 EXTI_ClearITPendingBit(EXTI_Line2);  //清除LINE2上的中断标志位
}